package com.wyg.epredisstarter.config;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.*;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.util.HashMap;


/**
 * Description: redis--cache配置，需要手动配置开启，{@link ConditionalOnProperty}
 *
 * @Author: WangYongGan
 * @Date: 2022/5/19 21:18
 * @Description:
 */
@Configuration
@ConditionalOnProperty(prefix = "ep.redis", name = "enable-method-cache", havingValue = "true", matchIfMissing = false)
@EnableCaching //开启注解
@Slf4j
public class CacheConfig extends CachingConfigurerSupport {

    /**
     * Description: 生成redis-key的策略
     *
     * @param
     * @return
     * @author WangYongGan
     * @see org.springframework.cache.annotation.CacheConfig
     * @see org.springframework.cache.annotation.Cacheable
     * @see org.springframework.cache.annotation.CachePut
     * @see org.springframework.cache.annotation.CacheEvict
     **/
    @Bean
    public KeyGenerator keyGenerator() {
        log.info("ep.redis.cache配置成功");
        return (target, method, params) -> {
            StringBuffer sb = new StringBuffer();
            sb.append(target.getClass().getName());
            sb.append(method.getName());
            for (Object obj : params) {
                sb.append(obj.toString());
            }
            return sb.toString();
        };
    }


}
